// pages/report/report.js
import searchReportByBarCode from "../../api/searchReportByBarCode";
import RunData from "../../config/runData";
import reportDetal from "../../api/url/reportDetal";
var Base64 = require("../../utils/base64");

Page({
    /**
     * 页面的初始数据
     */
    data: {
        hos: undefined,
        barcode: undefined,
        choiced: -1,
        choicedHos: "请选择医院"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            hos:RunData.hos
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },
    choiceHos: function (item) {
        if(item.detail.value==-1||!this.data.hos||this.data.hos.length<=item.detail.value){
            return
        }
        if (item.type === "change") {
            this.setData({
                choiced: item.detail.value,
                choicedHos: this.data.hos[item.detail.value].hospitalName
            })
        }
    },
    inputBarcode: function (e) {
        this.setData({
            barcode: e.detail.value
        });
    },
    scanBarcode: function () {
        // 只允许从相机扫码
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                this.setData({
                    barcode: res.result
                })
            }
        });
    },
    quary: function () {
        if (!RunData.MyUserInfo) {
            wx.showToast({title: '请先登录', icon: 'none'});
            return
        }
        if (!this.data.hos[this.data.choiced] || !this.data.hos[this.data.choiced].hospitalCode) {
            wx.showToast({title: '请先选择医院', icon: 'none'});
            return
        }
        if (!this.data.barcode) {
            wx.showToast({title: '请输入报告单编号', icon: 'none'});
            return
        }
        wx.showLoading({
            title: '查询中',
        });
        new searchReportByBarCode(
            RunData.MyUserInfo.name,
            this.data.hos[this.data.choiced].hospitalCode,
            this.data.barcode
        ).http((data) => {
            var listName = data.body.map((item) => {
                return item.reportType;
            });
            if (data.body.length > 1) {
                wx.showActionSheet({
                    itemList: listName,
                    success: function (c) {
                        var u = new reportDetal(hosCode,RunData.MyUserInfo.name,data.body[c.tapIndex].reportId).getUrl();
                        wx.navigateTo({
                            url: ('../web/web?toUrl='+Base64.encodeStr(u))
                        });
                    }
                })
            } else {
                var u = new reportDetal(hosCode,RunData.MyUserInfo.name,data.body[0].reportId).getUrl();
                wx.navigateTo({
                    url: ('../web/web?toUrl='+Base64.encodeStr(u))
                });
            }
        });
    }
});